package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_HPUpdate;
import com.lineage.server.serverpackets.S_MPUpdate;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class RewardArmorTable {
  private static ArrayList<ArrayList<Object>> aData15b = new ArrayList<>();
  
  private static boolean NO_MORE_GET_DATA15b = false;
  
  private static RewardArmorTable _instance;
  
  public static RewardArmorTable getInstance() {
    if (_instance == null)
      _instance = new RewardArmorTable(); 
    return _instance;
  }
  
  public static void forArmor(L1PcInstance pc, L1ItemInstance item, int weapon_level, boolean equipped) {
    ArrayList<?> aTempData = null;
    if (!NO_MORE_GET_DATA15b) {
      NO_MORE_GET_DATA15b = true;
      getData15b();
    } 
    int i = 0;
    while (i < aData15b.size()) {
      aTempData = aData15b.get(i);
      if (equipped) {
        if (weapon_level == ((Integer)aTempData.get(0)).intValue()) {
          if (((Integer)aTempData.get(1)).intValue() != 0) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\aL物理减伤增加:" + ((Integer)aTempData.get(1)).intValue()));
            pc.add_reduction_dmg(((Integer)aTempData.get(1)).intValue());
          } 
          if (((Integer)aTempData.get(2)).intValue() != 0) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\aL魔法减伤增加:" + ((Integer)aTempData.get(2)).intValue()));
            pc.add_magic_reduction_dmg(((Integer)aTempData.get(2)).intValue());
          } 
          if (((Integer)aTempData.get(3)).intValue() != 0) {
            pc.addMaxHp(((Integer)aTempData.get(3)).intValue());
            pc.sendPackets((ServerBasePacket)new S_HPUpdate(pc.getCurrentHp(), pc.getMaxHp()));
            pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\aL血量增加+" + ((Integer)aTempData.get(3)).intValue()));
            if (pc.isInParty())
              pc.getParty().updateMiniHP(pc); 
          } 
          if (((Integer)aTempData.get(4)).intValue() != 0) {
            pc.addMaxMp(((Integer)aTempData.get(4)).intValue());
            pc.sendPackets((ServerBasePacket)new S_MPUpdate(pc.getCurrentMp(), pc.getMaxMp()));
            pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\aL魔量增加+" + ((Integer)aTempData.get(4)).intValue()));
          } 
        } 
      } else if (weapon_level == ((Integer)aTempData.get(0)).intValue()) {
        if (((Integer)aTempData.get(1)).intValue() != 0) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\aL物理减伤减少:" + ((Integer)aTempData.get(1)).intValue()));
          pc.add_reduction_dmg(-((Integer)aTempData.get(1)).intValue());
        } 
        if (((Integer)aTempData.get(2)).intValue() != 0) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\aL魔法减伤减少:" + ((Integer)aTempData.get(2)).intValue()));
          pc.add_magic_reduction_dmg(-((Integer)aTempData.get(2)).intValue());
        } 
        if (((Integer)aTempData.get(3)).intValue() != 0) {
          pc.addMaxHp(-((Integer)aTempData.get(3)).intValue());
          pc.sendPackets((ServerBasePacket)new S_HPUpdate(pc.getCurrentHp(), pc.getMaxHp()));
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\aL血量减少:" + ((Integer)aTempData.get(3)).intValue()));
          if (pc.isInParty())
            pc.getParty().updateMiniHP(pc); 
        } 
        if (((Integer)aTempData.get(4)).intValue() != 0) {
          pc.addMaxMp(-((Integer)aTempData.get(4)).intValue());
          pc.sendPackets((ServerBasePacket)new S_MPUpdate(pc.getCurrentMp(), pc.getMaxMp()));
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\aL魔量减少:" + ((Integer)aTempData.get(4)).intValue()));
        } 
      } 
      i++;
    } 
  }
  
  private static void getData15b() {
    Connection con = null;
    try {
      con = DatabaseFactory.get().getConnection();
      Statement stat = con.createStatement();
      ResultSet rset = stat.executeQuery("SELECT * FROM w_过安定防具");
      ArrayList<Object> aReturn = null;
      if (rset != null)
        while (rset.next()) {
          aReturn = new ArrayList();
          aReturn.add(0, new Integer(rset.getInt("Level")));
          aReturn.add(1, new Integer(rset.getInt("Dmg_r")));
          aReturn.add(2, new Integer(rset.getInt("Dmg_Magic_r")));
          aReturn.add(3, new Integer(rset.getInt("Hp")));
          aReturn.add(4, new Integer(rset.getInt("Mp")));
          aData15b.add(aReturn);
        }  
      if (con != null && !con.isClosed())
        con.close(); 
    } catch (Exception exception) {}
  }
}
